//
//  StringVC.swift
//  swiftDemo
//
//  Created by yaqiang bi on 2017/3/8.
//  Copyright © 2017年 毕小强. All rights reserved.
//

import UIKit

class StringVC: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = UIColor.white
       
        
        /// 判断字符串是否为空的方法
        var emptyStirng = ""
        if emptyStirng.isEmpty {
            print("emptyStirng is nothing")
        }
        
        emptyStirng += "哈哈哈哈"
        
        ///遍历字符串
        for character in emptyStirng.characters {
            print(character)
        }
        
        ///创建字符
        let character :Character = "!"
        print(character)
        
        
        /// Sting值可以通过传入Character值的数组来初始化
        let catCharacters :[Character] = ["C","a","t","!"]
        let catString = String(catCharacters)
        print(catString)
        
        
        /// 字符串拼接
        var welcome:String = ""
        let exclamationMark :Character = "!"
        welcome.append(exclamationMark)
        print(welcome)
        
        
        /// 字符串拼接
        let multiplier = 3
        let message = "\(multiplier) is \(Double(multiplier))"
        print(message)
        
        
        /// 字符串字面量中的特殊字符
        let wiseWords = "\"这里有个双引号\"这里有个空字符\0这里有个反斜杠\\这里会换行\n这里有个水平制表符\t这里有回车\r"
        print(wiseWords)
        print("\u{24}")
        print("\u{2665}")
        let eAcute :Character = "\u{E9}"
        let combinedEAcute : Character = "\u{65}\u{301}"
        print(eAcute)
        print(combinedEAcute)
        
        
        /// 字符统计
        var word:String = "cafe"
        print("the number of characters in \(word) is \(word.characters.count)")
        word += "\u{24}"
        print("the number of characters in \(word) is \(word.characters.count)")
        
        
        /// 字符串索引
        let greeting = "Guten Tag!"
        let start = greeting[greeting.startIndex]
        print("start's first character is \(start)")
        let end = greeting[greeting.index(before: greeting.endIndex)]
        print("start's last character is \(end)")
        let index = greeting.index(greeting.startIndex, offsetBy: 7)
        print("the seventh character is \(greeting[index])")
        for index in greeting.characters.indices {
            print("\(greeting[index])")
        }
        
        
        /// 字符串插入
        var mutablStr = "hello"
        mutablStr.insert("!", at: mutablStr.endIndex)
        mutablStr.insert("w", at: mutablStr.startIndex)
        print(mutablStr)
        mutablStr.insert(contentsOf:" world".characters, at: mutablStr.index(before: mutablStr.endIndex))
        print(mutablStr)
        
        /// 字符串删除
        mutablStr.remove(at: mutablStr.index(before: mutablStr.endIndex))
        print(mutablStr)
        let range = mutablStr.index(mutablStr.endIndex, offsetBy: -6)..<mutablStr.endIndex
        mutablStr.removeSubrange(range)
        
        
        /// 字符串前缀和后缀的相等性
        let romeoAndJuliet = [
            "Act 1 Scene 1: Verona, A public place",
            "Act 1 Scene 2: Capulet's mansion",
            "Act 1 Scene 3: A room in Capulet's mansion",
            "Act 1 Scene 4: A street outside Capulet's mansion",
            "Act 1 Scene 5: The Great Hall in Capulet's mansion",
            "Act 2 Scene 1: Outside Capulet's mansion",
            "Act 2 Scene 2: Capulet's orchard",
            "Act 2 Scene 3: Outside Friar Lawrence's cell",
            "Act 2 Scene 4: A street in Verona",
            "Act 2 Scene 5: Capulet's mansion",
            "Act 2 Scene 6: Friar Lawrence's cell"
        ]
        
        var actCount = 0
        var cellCount = 0
        for str in romeoAndJuliet {
            if str.hasPrefix("Act 1") {
                actCount += 1
            }
            if str.hasSuffix("cell") {
                cellCount += 1
            }
        }
        print("There are \(actCount) Act 1")
        print("There are \(cellCount) cell")
        
        
        /// utf编码
        let utfStr = "one dog 😄"
        for codeUnit in utfStr.utf8 {
            print(codeUnit)
        }
        for codeUnit in utfStr.utf16 {
            print(codeUnit)
        }
        for scalar in utfStr.unicodeScalars {
            print(scalar)
        }
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
